package com.itheima.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;


@Component
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        long id = Thread.currentThread().getId();
        log.info("线程id为：{}",id);
        log.info("公共字段自动填充[insert]...");
        log.info(metaObject.toString());
        log.info("当前时间：{}",LocalDateTime.now());
        this.setFieldValByName("createTime",LocalDateTime.now(),metaObject);
        this.setFieldValByName("updateTime",LocalDateTime.now(),metaObject);
        this.setFieldValByName("createUser",ThreadLocalUtil.getCurrentId(),metaObject);
        this.setFieldValByName("updateUser",ThreadLocalUtil.getCurrentId(),metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("公共字段自动填充[insert]...");
        log.info(metaObject.toString());
        this.setFieldValByName("updateTime",LocalDateTime.now(),metaObject);
        this.setFieldValByName("updateUser",ThreadLocalUtil.getCurrentId(),metaObject);
    }
}
